package com.wzx.core.aop.invoke;

import com.wzx.core.aop.annotaction.Throwing;
import com.wzx.core.ioc.bean.ObjectFactory;
import com.wzx.core.ioc.context.AbstractApplicationContext;
import com.wzx.core.ioc.context.AnnotationApplicationContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AspectLogic {

    private Object aspectObject;
    private Method aspectMethod;
    private AbstractApplicationContext context;
    private String aspectObjectName;

    private ObjectFactory aspectObjectFactory;

    public AspectLogic(ObjectFactory aspectObjectFactory, Method aspectMethod) {
        this.context = context;
        this.aspectObjectName = aspectObjectName;
        this.aspectMethod = aspectMethod;
        this.aspectObjectFactory = aspectObjectFactory;
    }

    public void proceed(Object o, Method method, Object[] objects, Object retVal, Throwable ex) {
        JoinPoint joinPoint = new JoinPoint();
        joinPoint.setOrigin(o);
        joinPoint.setMethod(method);
        joinPoint.setArgs(objects);
        joinPoint.setRetVal(retVal);
        joinPoint.setThrowable(ex);
        try {
            aspectMethod.invoke(getAspectObject(), joinPoint);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public Object getAspectObject() {
        if (aspectObject == null) {
            aspectObject = aspectObjectFactory.getObject();
        }
        return aspectObject;
    }
}
